// pages/groundPush/groundPushIndex.js
const common = require('../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navType: 0,
        list: [{
                page: 0,
                limit: 10,
                data: [],
                loadingStatus: true
            },
            {
                page: 0,
                limit: 10,
                data: [],
                loadingStatus: true
            }
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        that.getFictitiousList();
    },
    goGoodsDetail: function(e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id,
        })
    },
    goGroundPush: function(e) {
        wx.navigateTo({
            url: '/pages/groundPush/groundPush_details/groundPush_details?id=' + e.currentTarget.dataset.id,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 1000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        let that = this;
        let navType = that.data.navType;
        let page = that.data.list[navType].page;
        let loadingStatus = that.data.list[navType].loadingStatus;
        if (loadingStatus) {
            page++;
            console.log(page)
            that.setData({
                ["list[" + navType + "].page"]: page
            })
            that.getFictitiousList();
        }
    },
    getGoodsList: function() {
        let that = this;
        let fictitious = that.data.list[1].data; //数据
        let page = that.data.list[1].page; //页数
        let loadingStatus = that.data.list[1].loadingStatus; // 判断是否执行上拉加载
        let limit = that.data.list[1].limit; //条数
        if (!loadingStatus) {
            return false
        }
        let data = {
            activity: 785,
            limit: limit,
            page: page,
            is_goods: 2,
            client: 1
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Goodslist/goods_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                if (res.error === "暂无数据") {
                    that.setData({
                        ["list[1].loadingStatus"]: false
                    })
                }
                return false
            } else {
                if (res.length < limit) { // 上拉判断
                    loadingStatus = false;
                }
                fictitious = fictitious.concat(res);
                that.setData({
                    ["list[1].data"]: fictitious,
                    ["list[1].loadingStatus"]: loadingStatus
                })
                return false
            }

        }).catch(function(e) {})
    },
    getFictitiousList: function() {
        let that = this;
        let fictitious = that.data.list[0].data; //数据
        let page = that.data.list[0].page; //页数
        let loadingStatus = that.data.list[0].loadingStatus; // 判断是否执行上拉加载
        let limit = that.data.list[0].limit; //条数
        if (!loadingStatus) {
            return false
        }
        let data = {
            activity: 785,
            type: 5,
            limit: limit,
            page: page
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Fodder/fodder_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                if (res.error === "暂无数据") {
                    that.setData({
                        ["list[0].loadingStatus"]: false
                    })
                }
                return false
            } else {
                if (res.length < limit) { // 上拉判断
                    loadingStatus = false;
                }
                fictitious = fictitious.concat(res);
                that.setData({
                    ["list[0].data"]: fictitious,
                    ["list[0].loadingStatus"]: loadingStatus
                })
                return false
            }

        }).catch(function(e) {})
    },

    navClick: function(e) {
        let type = e.currentTarget.dataset.type;
        if (this.data.navType == type) {
            return
        }
        this.setData({
            navType: type
        })
        if (type === "1") {
            this.getGoodsList();
        } else if (type === "0") {
            this.getFictitiousList();
        }

    }
})